<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\OrderTab;
use App\Models\Pay;
use App\Models\StoreGoods;
use App\Models\StoreOrder;
use App\Service\CommonService;
use App\Service\Validate;

class OrderController extends Controller
{
    /**
     * User: YKY
     * DateTime: 2022/11/14 20:25
     * @return array
     * 修改订单状态
     */
    public function updateOrder()
    {
        //获取订单id
        $oid = intval(request()->get('oid'));
        $uid = intval(request()->get('uid'));
        //根据订单id查询数据
        $list = StoreOrder::where(['id' => $oid])->update(['status' => '2']);
        //判断订单状态是否修改成功
        if ($list) return CommonService::success(200, '订单状态修改成功', $list);
        return CommonService::fail(500, '订单状态修改失败', []);
    }

    /**
     * User: YKY
     * DateTime: 2022/11/14 20:37
     * @return array
     * 删除订单接口
     */
    public function delOrder()
    {
        //获取订单id
        $oid = intval(request()->get('oid'));
        //根据订单id删除数据
        $list = StoreOrder::where('id', $oid)->delete();
        //判断是否删除完成
        if ($list == 1) return CommonService::success(200, '数据删除成功', []);
        return commonService::fail(9999, '数据删除失败', []);
    }

    /**
     * User: YKY
     * DateTime: 2022/11/16 10:04
     * @return array
     * 订单列表接口
     * $status订单状态
     */
    public function orderList()
    {
        $status = request()->input('status');
        $uid = request()->input('uid');
        if (empty($status)) {
            $orderData = StoreOrder::with(['getStatus','getGoods'])->where(['uid' => $uid])->get();
            return CommonService::success(200, '查询成功', $orderData);
        }
        if ($status == 0) {
            $orderData = StoreOrder::with(['getStatus','getGoods'])->where(['uid' => $uid])->with(['getStatus'])->get();
            return CommonService::success(200, '查询成功', $orderData);
        }
        $orderData = $orderData = StoreOrder::with(['getStatus','getGoods'])->where(['uid' => $uid, 'status' => $status])->get();
        return CommonService::success(200, '查询成功', $orderData);
    }

    /**
     * User: YKY
     * DateTime: 2022/11/15 10:54
     * 订单创建接口
     */
    public function createOrder()
    {
        $addOrder = request()->input();
        $file = [
        ];
        $msg = [
        ];
        $validate = Validate::store($addOrder, $file, $msg);
        if ($validate) return CommonService::fail(200, $validate, '');
        $goods=StoreGoods::where('id',$addOrder['gid'])->first();
        $addData = [
            'order_id' => mt_rand(1000, 9999) . strtotime(date('Ymdhis')),
            'uid' => $addOrder['uid'],
            'order_name'=>$goods['store_name'],
            'pay_price'=>$addOrder['pay_price'],
            'goods_id'=>$addOrder['gid'],
            'user_address' => '',
            'pay_type' => '',
            'mark' => '',
            'status'=>'1'
        ];
        try {
            $createStatus = StoreOrder::insertGetId($addData);
            if ($createStatus) return CommonService::success(200, '订单创建成功', '');
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage(), '');
        }
    }

    public function tabList()
    {
        $tab = OrderTab::limit(5)->get();
        return CommonService::success(200, '', $tab);
    }

    /**
     * User: YKY
     * DateTime: 2022/11/17 20:50
     * @return array
     * 支付方式
     */
    public function payList()
    {
        try {
            $pay = Pay::get();
            if (empty($pay)) return CommonService::fail(9999, 'null', '');
            return CommonService::success(200, '查询成功', $pay);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage(), '');
        }
    }

    /**
     * User: YKY
     * DateTime: 2022/11/17 20:52
     * @return array
     * 订单详情
     */
    public function orderInfo()
    {
        $oid = request()->input('oid');
        try {
            $info = StoreOrder::where('id', $oid)->first();
            if (empty($info)) return CommonService::fail(9999, 'null', '');
            return CommonService::success(200, '查询成功', $info);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage(), '');
        }
    }

    public function orderDetail()
    {
        $uid = intval(request()->get('uid'));
        $orderId = intval(request()->get('order_id'));

        try {
            $orderData = Order::where('id',$orderId)->where('uid',$uid)->get()->toArray();
            if(!$orderData)return CommonService::fail(9999,'无数据', '');
            return CommonService::success(200,'订单数据', $orderData);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage(), '');
        }

    }
    public function orderOvertime(){
        $time=strtotime(date('Y-m-d h:i:s'));
        $orderData=StoreOrder::get();
        $arr1=json_decode($orderData,true);
        $this->dispatch(new Queue($arr1));//进入队列
    }
}
